# coding:utf-8
import os
import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)


# 获取指定目录下 指定类型的文件
def get_file_name_list(file_dir, file_type):
    file_name_list = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if os.path.splitext(file)[1] == ('.' + file_type):
                file_name_list.append(os.path.join(root, file))
    return file_name_list


def file_size(root, file):
    return os.path.getsize(os.path.join(root, file))


# def rename_file(root_dir, old_name, file_type):
#     ole_file = old_name + file_type
#     new_file = old_name[0:npos].strip()
#     new_name = new_file + file_type
#     # try:
#     print('修改前：' + old_name)
#     print('修改后：' + new_name)
#     # 判断文件是否存在
#     i = 1
#     while os.path.exists(os.path.join(root_dir, new_name)):
#         # 判断存在的文件是否相同，相同则删除
#         if os.path.getsize(os.path.join(root_dir, ole_file)) == os.path.getsize(os.path.join(root_dir, new_name)):
#             os.remove(os.path.join(root_dir, ole_file))
#             delete_files.append(os.path.join(root_dir, ole_file))
#             continue
#         else:
#             logger.info(new_name + "已存在！处理中。。。")
#             new_name = new_file + '_' + str(i) + file_type
#             i += 1
#     # else:
#     if os.path.exists(os.path.join(root_dir, ole_file)):
#         os.rename(os.path.join(root_dir, ole_file), os.path.join(root_dir, new_name))
#
#     print('-----  修改完成 ---------')


def rename_files(file_path):
    delete_files = []
    for root_dir, dirs, files in os.walk(filePath):
        for file in files:
            # print(file)
            old_name, file_type = os.path.splitext(file)
            index = -1
            if old_name.find('Chinese') > 0:
                index = old_name.find('Chinese')

            elif old_name.find('_偷拍自拍') > 0:
                index = old_name.find('_偷拍自拍')
            elif old_name.find('-') > 0:
                index = old_name.find('-')
            if index == -1:
                continue
            new_file = old_name[0:index].strip()
            new_name = new_file + file_type
            i = 1
            file_exists = os.path.exists(os.path.join(root_dir, new_name))
            while file_exists:
                # 判断存在的文件是否相同，相同则删除
                if file_size(root_dir, file) == file_size(root_dir, new_name):
                    os.remove(os.path.join(root_dir, file))
                    delete_files.append(os.path.join(root_dir, file))
                    file_exists = False
                    logger.info("删除已存在的文件" + str(os.path.join(root_dir, file)))
                else:
                    logger.info(new_name + "已存在！处理中。。。")
                    new_name = new_file + '_' + str(i) + file_type
                    i += 1
                    file_exists = os.path.exists(os.path.join(root_dir, new_name))
            # else:
            if os.path.exists(os.path.join(root_dir, file)):
                os.rename(os.path.join(root_dir, file), os.path.join(root_dir, new_name))

            # print('-----  修改完成 ---------')
    if len(delete_files) > 0:
        with open('删除的重复文件.txt', 'w', encoding='utf-8') as f:
            for item in delete_files:
                f.write(item + '\n')


if __name__ == '__main__':
    # 用户路径
    user_dir = os.path.expanduser('~') + os.sep
    print(user_dir)
    # filePath = user_dir + "Videos"
    filePath = user_dir + "Downloads" + os.sep
    # filePath = user_dir + "Pictures" + os.sep + 'Camera Roll' + os.sep
    # C:\Users\Augurit\Pictures\Camera Roll

    rename_files(filePath)
